package com.clouddot.baipai.ui.fragment.V;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.clouddot.baipai.R;
import com.clouddot.baipai.component.DaggerIV_SchoolFragmentComponent;
import com.clouddot.baipai.contract.ISchoolFragmentContract;
import com.clouddot.baipai.http.entity.response.getArcticListResponse;
import com.clouddot.baipai.http.entity.response.getSchoolInfoResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.module.SchoolFragmentModule;
import com.clouddot.baipai.ui.activity.V_SMainActivity;
import com.clouddot.baipai.util.viewUtil.AlphaAndScaleTransformer;
import com.clouddot.baipai.ui.adapter.V_SchoolsAdapter;
import com.clouddot.baipai.util.Toast.ToastUtil;
import com.clouddot.baipai.util.viewUtil.ViewAnim;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;

import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.impl.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by wxy on 2018/8/23.
 * 横版班牌学校的主页面
 */

public class V_SchoolFrgment extends BaseFragment implements ISchoolFragmentContract.IView, ViewPager.OnPageChangeListener {
    private List<getArcticListResponse.ariticInfo> mDataList = new ArrayList<>();
    private List<getSchoolInfoResponse> mDataInfoList = new ArrayList<>();
    private V_SchoolsAdapter schoolsAdapter;
    private ImageView[] mBottomImages;
    private int currentViewPagerItem = -1;
    private boolean isHide = false;

    @Inject
    ISchoolFragmentContract.IPresenter mPresenter;
    @BindView(R.id.Viewpager)
    ViewPager Viewpager;
    @BindView(R.id.indicator)
    LinearLayout indicator;

    @BindView(R.id.loadingLF)
    RelativeLayout loadingLF;
    @BindView(R.id.LoadingL)
    ImageView LoadingL;

    @Override
    public IBasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.v_fragment_school, null);
    }

    @Override
    protected void initViews() {
        DaggerIV_SchoolFragmentComponent.builder().schoolFragmentModule(new SchoolFragmentModule(this)).build().inject(this);
        mPresenter.getArcticeInfo("SchoolInfo", 1, 10);
    }

    @Override
    protected void initVariables() {
        Animation loadingAnim = ViewAnim.getRotateImg(getActivity());
        LoadingL.startAnimation(loadingAnim);
        Viewpager.setOnPageChangeListener(this);
    }


    @Override
    protected void loadData() {

    }
    /**
     * fragment 隐藏和显现触发的方法，第一次生成的时候不会触发。
     *  hidden 为 false --> 显现 ；为 true --> 隐藏
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            isHide = false;
            loadingLF.setVisibility(View.VISIBLE);
            Viewpager.setVisibility(View.GONE);
            mDataList.clear();
            mDataInfoList.clear();
            mPresenter.getArcticeInfo("SchoolInfo", 1, 10);
        } else {
            isHide = true;
            NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
        }
    }

    /**
     *
     * @param flag 1 --> 获取学校item的内容
     *             2 --> 获取学校item的列表
     * @param object
     */
    @Override
    public void success(int flag, Object object) {
        if (flag == 1) {
            refluesShcoolInfo(object);
        } else if (flag == 2) {
            refluesArcticeList(object);
        }
    }

    @Override
    public void fail(String errMsg) {
        ToastUtil.showToast(getActivity(), errMsg);
    }


    /**
     *
     * 更新学校item内容
     * @param object
     */

    private void refluesShcoolInfo(Object object) {
        mDataInfoList.add((getSchoolInfoResponse) object);
        if (mDataInfoList.size() == mDataList.size() && !isHide) {
            loadingLF.setVisibility(View.GONE);
            Viewpager.setVisibility(View.VISIBLE);
            schoolsAdapter = new V_SchoolsAdapter(getActivity(), mDataInfoList);
            Viewpager.setAdapter(schoolsAdapter);
            Viewpager.setPageMargin(10);
            Viewpager.setOffscreenPageLimit(3);
            Viewpager.setPageTransformer(false, new AlphaAndScaleTransformer());
            loadingLF.setVisibility(View.GONE);
            Viewpager.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 更新学校item列表数据
     * @param object
     */
    private void refluesArcticeList(Object object) {
        final getArcticListResponse response = (getArcticListResponse) object;
        if (response.EntityList.isEmpty()) return;
        mDataList = response.EntityList;
        for (getArcticListResponse.ariticInfo ariticInfo : mDataList)
            mPresenter.getScholInfo(ariticInfo.Title);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        V_SMainActivity.backTime = Env.getAppContext().backToMainTime * 60;
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

}
